package com.wujunshen.algorithm.leetcode.basic;

/**
 * @author frank woo(吴峻申) <br>
 *     email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2022/7/25 01:32<br>
 */
public class 有序数组的平方 {
  public int[] sortedSquares(int[] nums) {
    int[] result = new int[nums.length];

    int low = 0;
    int high = nums.length - 1;
    for (int i = nums.length - 1; i >= 0; i--) {
      if (Math.abs(nums[low]) >= Math.abs(nums[high])) {
        result[i] = nums[low] * nums[low];
        low++;
      } else {
        result[i] = nums[high] * nums[high];
        high--;
      }
    }
    return result;
  }
}
